package com.example.szl7033.camera_1_convert;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "shi_main";

    @SuppressLint("SdCardPath")
    File tempFile = new File("/sdcard/a_960x720.nv21");
    @SuppressLint("SdCardPath")
    File yFile = new File("/sdcard/y_960x720.nv21");
    @SuppressLint("SdCardPath")
    File uFile = new File("/sdcard/u_960x720.nv21");
    @SuppressLint("SdCardPath")
    File vFile = new File("/sdcard/v_960x720.nv21");

    //Android6.0 以上需要运行时权限授权
    private void checkPermission(){
        String[] permission = new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };
        List<String> mPermissionList = new ArrayList<>();
        mPermissionList.clear();
        for (int i = 0; i < permission.length; i++) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, permission[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permission[i]);
            }
        }
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
            Toast.makeText(MainActivity.this,"已经授权",Toast.LENGTH_LONG).show();
        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permissions, 2);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermission();

        Button button_y = findViewById(R.id.button_to_y);
        button_y.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Log.d(TAG,"onClick");
        switch (view.getId()){
            case R.id.button_to_y : get_y();break;  //执行转换函数
            default:Log.d(TAG,"default");
        }
    }

    public void get_y() {
        //Log.d(TAG,"get in getY");
        if(! yFile.exists()){
            try {
                yFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                Log.d(TAG,"create file fail");
            }
        }
        if(tempFile.exists()){
            try{
                FileInputStream in = new FileInputStream(tempFile); //打开文件输入流
                RandomAccessFile randomFile = new RandomAccessFile(yFile,"rw");  //若文件存在，则以追加的方式写入数据
                long fileLength = randomFile.length();
                randomFile.seek(fileLength);

                byte[] buffer = new byte[960*720*3/2];  //设置帧缓冲区
                int len = in.read(buffer);
                while(len > 0){
                    //每帧数据变换
                    //Log.d(TAG,"start");
                    int framesize = 960*720;
                    int i;
                    for(i = framesize;i<framesize*3/2;i++){
                        buffer[i] = (byte) 0x80;    //设置UV,125灰度图
                    }
                    randomFile.write(buffer);

                    len = in.read(buffer);
                    //Log.d(TAG,"each_frame");
                }
                randomFile.close();
                in.close();
                //Log.d(TAG,"convert_succeeded");
                Toast.makeText(this,"convert succeeded!",Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.d(TAG,"open_file_fail");
            } catch (IOException e) {
                e.printStackTrace();
                Log.d(TAG,"read_fail");
            }
        } else{
            Log.d(TAG,"file is unexists");
            //Toast.makeText(this,"file is unexists",Toast.LENGTH_SHORT).show();
        }
    }

}
